package phase01.module4.code.question1;

import phase01.module4.code.question1.exceptions.InvalidAgeException;
import phase01.module4.code.question1.exceptions.InvalidIdException;

import java.io.Serializable;
import java.util.Objects;

public class Student implements Serializable {
    private int id;
    private String name;
    private int age;
    private String gender;

    public Student() {
    }

    public Student(int id, String name, int age, String gender) throws InvalidIdException, InvalidAgeException {
        setId(id);
        setName(name);
        setAge(age);
        setGender(gender);
    }

    @Override
    public String toString() {
        return "学生{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) throws InvalidIdException {
        if(id < 0) {
            throw new InvalidIdException("错误：无效的学号！");
        }
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws InvalidAgeException {
        if(age < 0 || age > 150) {
            throw new InvalidAgeException("错误：无效的年龄！");
        }
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}
